package com.jinxin.platform.cdfaceacs.service.impl;

import com.jinxin.platform.cdfaceacs.common.CommonCodeEntity;
import com.jinxin.platform.cdfaceacs.common.ResultEntity;
import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlBlockInfoDO;
import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlRoomInfoDO;
import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlUserInfoDO;
import com.jinxin.platform.cdfaceacs.pojo.entity.Constant;
import com.jinxin.platform.cdfaceacs.service.AcsControlBlockInfoService;
import com.jinxin.platform.cdfaceacs.service.AcsControlRoomInfoService;
import com.jinxin.platform.cdfaceacs.service.AcsControlUserInfoService;
import com.jinxin.platform.cdfaceacs.service.AcsExportService;
import com.jinxin.platform.cdfaceacs.utils.ExportExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-10-27
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class AcsExportServiceImpl implements AcsExportService {
    @Autowired
    private AcsControlBlockInfoService blockInfoService;
    @Autowired
    private AcsControlRoomInfoService roomInfoService;
    @Autowired
    private AcsControlUserInfoService userInfoService;


    @Override
    public ResultEntity exportBlock2Excel(HttpServletResponse response, String communityId) {
        if(StringUtils.isEmpty(communityId)){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"小区id为空");
        }
        AcsControlBlockInfoDO blockInfoDO = new AcsControlBlockInfoDO();
        blockInfoDO.setCompanyId(communityId);
        List<AcsControlBlockInfoDO> blockInfoList = blockInfoService.selDOByInfo(blockInfoDO);
        if(blockInfoList == null || blockInfoList.size() == 0){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"无数据");
        }
        String fileName = "楼栋信息表.xls";
        try {
            fileName = new String(fileName.getBytes("GB2312"), "ISO_8859_1");
        } catch (UnsupportedEncodingException e) {
            log.error("error",e);
            return new ResultEntity(CommonCodeEntity.FAIL,"导出楼栋信息字符错误，请联系管理员");
        }
        response.setHeader("Content-Disposition", "attachment; filename=".concat(fileName));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/vnd.ms-excel");
        response.setContentType("application/x-download;charset=UTF-8");
        String[] headers = {"楼栋编号", "楼栋名称","楼栋描述","楼栋地址", "层数", "类型"};
        List<List<String>> resultData = new ArrayList<>();
        for (AcsControlBlockInfoDO blockInfo : blockInfoList) {
            List<String> fieldList = new ArrayList<>();
            fieldList.add(blockInfo.getBlockNo());
            fieldList.add(blockInfo.getBlockName());
            fieldList.add(blockInfo.getBlockDesc());
            fieldList.add(blockInfo.getBlockAddress());
            fieldList.add(blockInfo.getBlockFloor()+"");
            String blockType = blockInfo.getBlockType() != null && blockInfo.getBlockType() == 1 ? "出入口" : "楼栋"; //  1出入口2 楼栋
            fieldList.add(blockType);
            resultData.add(fieldList);
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        OutputStream ouputStream = null;
        try {
            ouputStream = response.getOutputStream();
            ExportExcelUtils.exportExcel(workbook,0,"楼栋信息",headers,resultData);
            workbook.write(ouputStream);
        } catch (Exception e) {
            log.error("导出楼栋信息出错", e);
            e.printStackTrace();
            return new ResultEntity(CommonCodeEntity.FAIL,"导出楼栋信息错误，请重试");
        }finally {
            if(ouputStream != null){
                try {
                    ouputStream.close();
                } catch (IOException e) {
                    log.error("error",e);
                }
            }
        }
        log.info("成功导出"+(blockInfoList.size())+"条楼栋信息");
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"成功导出"+(blockInfoList.size())+"条楼栋信息");
    }

    @Override
    public ResultEntity exportRoom2Excel(HttpServletResponse response, String communityId) {
        if(StringUtils.isEmpty(communityId)){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"小区id为空");
        }
        AcsControlRoomInfoDO roomInfoDO = new AcsControlRoomInfoDO();
        roomInfoDO.setCompanyId(communityId);
        List<AcsControlRoomInfoDO> roomInfoList = roomInfoService.selDOByInfo(roomInfoDO);
        if(roomInfoList == null || roomInfoList.size() == 0){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"无数据");
        }
        String fileName = "房间信息表.xls";
        try {
            fileName = new String(fileName.getBytes("GB2312"), "ISO_8859_1");
        } catch (UnsupportedEncodingException e) {
            log.error("error",e);
            return new ResultEntity(CommonCodeEntity.FAIL,"导出房间信息字符错误，请联系管理员");
        }
        response.setHeader("Content-Disposition", "attachment; filename=".concat(fileName));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/vnd.ms-excel");
        response.setContentType("application/x-download;charset=UTF-8");
        String[] headers = {"房间ID","房间号", "房间描述","楼栋ID","楼栋名称"};
        List<List<String>> resultData = new ArrayList<>();
        for (AcsControlRoomInfoDO roomInfo : roomInfoList) {
            List<String> fieldList = new ArrayList<>();
            fieldList.add(roomInfo.getRoomId());
            fieldList.add(roomInfo.getRoomName());
            fieldList.add(roomInfo.getRoomDesc());
            fieldList.add(roomInfo.getBlockId());
            fieldList.add(roomInfo.getBlockName());
            resultData.add(fieldList);
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        OutputStream ouputStream = null;
        try {
            ouputStream = response.getOutputStream();
            ExportExcelUtils.exportExcel(workbook,0,"房间信息",headers,resultData);
            workbook.write(ouputStream);
        } catch (Exception e) {
            log.error("导出房间信息出错", e);
            e.printStackTrace();
            return new ResultEntity(CommonCodeEntity.FAIL,"导出房间信息错误，请重试");
        }finally {
            if(ouputStream != null){
                try {
                    ouputStream.close();
                } catch (IOException e) {
                    log.error("error",e);
                }
            }
        }
        log.info("成功导出"+(roomInfoList.size())+"条房间信息");
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"成功导出"+(roomInfoList.size())+"条楼栋信息");
    }

    @Override
    public ResultEntity exportUser2Excel(HttpServletResponse response, String communityId) {
        if(StringUtils.isEmpty(communityId)){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"小区id为空");
        }
        AcsControlUserInfoDO userInfoDO = new AcsControlUserInfoDO();
        userInfoDO.setCompanyId(communityId);
        List<AcsControlUserInfoDO> userInfoList =
                userInfoService.selAllParam("","","","",communityId,"", Constant.USER_TYPE_MEMBER);
        if(userInfoList == null || userInfoList.size() == 0){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"无数据");
        }
        String fileName = "住户信息表.xls";
        try {
            fileName = new String(fileName.getBytes("GB2312"), "ISO_8859_1");
        } catch (UnsupportedEncodingException e) {
            log.error("error",e);
            return new ResultEntity(CommonCodeEntity.FAIL,"导出住户信息字符错误，请联系管理员");
        }
        response.setHeader("Content-Disposition", "attachment; filename=".concat(fileName));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/vnd.ms-excel");
        response.setContentType("application/x-download;charset=UTF-8");
        String[] headers = {"人员类型(11业主13租客)", "姓名","性别(1男2女)","证件类型(1身份证2护照3驾照4其他)","证件ID",
        "电话","房间ID","房间名称","楼栋ID","楼栋名称","人户一致(0人户一致、1人户分离、2流动人口)","党籍（0党员、1群众、2其他）","人脸照片"};
        List<List<String>> resultData = new ArrayList<>();
        for (AcsControlUserInfoDO userInfo : userInfoList) {
            List<String> fieldList = new ArrayList<>();
            fieldList.add(userInfo.getUserType());
            fieldList.add(userInfo.getUserName());
            fieldList.add(userInfo.getGender()+"");
            fieldList.add(userInfo.getUserCardType());
            fieldList.add(userInfo.getUserCard());
            fieldList.add(userInfo.getUserPhone());
            fieldList.add(userInfo.getUserRoomId());
            fieldList.add(userInfo.getUserRoomName());
            fieldList.add(userInfo.getUserBlockId());
            fieldList.add(userInfo.getUserBlockName());
            fieldList.add(userInfo.getHushaiConsistent()+"");
            fieldList.add(userInfo.getPartyMembership()+"");
            fieldList.add(userInfo.getUserFaceInfo()+"");
            resultData.add(fieldList);
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        OutputStream ouputStream = null;
        try {
            ouputStream = response.getOutputStream();
            ExportExcelUtils.exportExcel(workbook,0,"住户信息",headers,resultData);
            workbook.write(ouputStream);
        } catch (Exception e) {
            log.error("导出住户信息出错", e);
            e.printStackTrace();
            return new ResultEntity(CommonCodeEntity.FAIL,"导出住户信息错误，请重试");
        }finally {
            if(ouputStream != null){
                try {
                    ouputStream.close();
                } catch (IOException e) {
                    log.error("error",e);
                }
            }
        }
        log.info("成功导出"+(userInfoList.size())+"条住户信息");
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"成功导出"+(userInfoList.size())+"条住户信息");
    }
}
